package com.yhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhx.iservice.ICheckgroupService;
import com.yhx.mapper.CheckgroupCheckitemMapper;
import com.yhx.mapper.CheckgroupMapper;
import com.yhx.pojo.Checkgroup;
import com.yhx.pojo.CheckgroupCheckitem;
import com.yhx.utils.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class CheckgroupServiceImpl implements ICheckgroupService {

    @Resource
    private CheckgroupMapper checkgroupMapper;

    @Resource
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    @Override
    public PageResult list(QueryPageBean queryPageBean) {

        QueryWrapper<Checkgroup> queryWrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if(queryString!=null && queryString.length()>0){
            queryWrapper.like("code", queryString)
                    .or().like("name", queryString)
                    .or().like("helpCode", queryString);
        }
        Page<Checkgroup> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Checkgroup> checkgroupPage = checkgroupMapper.selectPage(page, queryWrapper);
        return new PageResult(checkgroupPage.getTotal(),checkgroupPage.getRecords());
    }


    @Override
    public Result save(Checkgroup checkgroup, Integer[] integers) {
        //设置助记码
        if(checkgroup.getHelpcode()==null ||checkgroup.getHelpcode().equals("")){
            String pinYinHeadChar = PinYin4jUtils.getPinYinHeadChar(checkgroup.getName());
            checkgroup.setHelpcode(pinYinHeadChar);
        }
        //先添加gourp表            mybatis-plus已经做好了获取主键值
        int insert = checkgroupMapper.insert(checkgroup);
        if(insert==0 || integers==null || integers.length==0){
            //添加失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //添加成功，添加中间表对象
        //先拿到检查组对象的id
        Integer id = checkgroup.getId();
        for (int i = 0; i < integers.length; i++) {
            //创建中间表对象
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem(id, integers[i]);
            //添加
            checkgroupCheckitemMapper.insert(checkgroupCheckitem);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @Override
    public Result getByid(int id) {
        List<Integer> checkitemList=new ArrayList<>();
        //查询这个检查项的信息
        Checkgroup checkgroup = checkgroupMapper.selectById(id);
        if (checkgroup==null){
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        //查找这个检查组有那些检查项
        QueryWrapper<CheckgroupCheckitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("checkgroup_id", id);
        List<CheckgroupCheckitem> list = checkgroupCheckitemMapper.selectList(queryWrapper);
        for (CheckgroupCheckitem checkgroupCheckitem : list) {
            Integer checkitemId = checkgroupCheckitem.getCheckitemId();
            checkitemList.add(checkitemId);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS,checkgroup,checkitemList);
    }

    @Override
    @Transactional //开启事务控制
    public Result update(Checkgroup checkgroup, Integer[] integers) {
        //设置助记码
        if(checkgroup.getHelpcode()==null ||checkgroup.getHelpcode().equals("")){
            String pinYinHeadChar = PinYin4jUtils.getPinYinHeadChar(checkgroup.getName());
            checkgroup.setHelpcode(pinYinHeadChar);
        }
        //先修改gourp表            mybatis-plus已经做好了获取主键值
        int update = checkgroupMapper.updateById(checkgroup);
        if(update==0 || integers==null || integers.length==0){
            //修改失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        //修改检查组包含的检查项
        //先删除原来的检查项
        QueryWrapper<CheckgroupCheckitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("checkgroup_id", checkgroup.getId());
        int delete = checkgroupCheckitemMapper.delete(queryWrapper);
        if(delete<=0){
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        //添加成功，添加中间表对象
        //先拿到检查组对象的id
        Integer id = checkgroup.getId();

        for (int i = 0; i < integers.length; i++) {
            //创建中间表对象
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem(id, integers[i]);
            //添加
            checkgroupCheckitemMapper.insert(checkgroupCheckitem);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @Override
    public Result delete(int id) {
        int i = checkgroupMapper.deleteById(id);
        if(i>0){
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
    }

    @Override
    public Result deleteAll(Integer[] ids) {
        List<Integer> integers = Arrays.asList(ids);
        int i = checkgroupMapper.deleteBatchIds(integers);
        if(i>0){
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
    }

    @Override
    public List<Checkgroup> listall() {
        return checkgroupMapper.selectList(null);
    }


}
